package com.example.wangzh.developkit.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.utils.UIUtils;
import com.example.wangzh.developkit.utils.ViewUtils;


/**
 * Created by USER on 2017/1/22.
 */

public class ClearEditText extends LinearLayout {

    public interface OnClearTextListener {
        void onClearText();
    }

    EditText edt;
    ImageView img;
    String hint;
    OnClearTextListener onClearTextListener;
    int textColor;
    int hintTextColor;
    int inputType;
    float text_margin_left;
    float text_margin_top;
    float text_margin_bottom;
    float text_size;

    public void setOnClearTextListener(OnClearTextListener onClearTextListener) {
        this.onClearTextListener = onClearTextListener;
    }

    public ClearEditText(Context context) {
        super(context);
        initView(context, null);
    }


    public ClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.custom_clear_edit_text, this);
        edt = (EditText) view.findViewById(R.id.edt);
        img = (ImageView) view.findViewById(R.id.img);
        img.setVisibility(View.INVISIBLE);
        textColor = edt.getCurrentTextColor();
        hintTextColor = edt.getCurrentHintTextColor();
        if (isInEditMode()) {
            return;
        }
        loadAttrs(context, attrs);
        img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                edt.setText("");
                if (onClearTextListener != null) {
                    onClearTextListener.onClearText();
                }
            }
        });
        edt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                ViewUtils.visibleOrInvisible(img, s.length() != 0);
            }
        });
        edt.setTextColor(textColor);
        edt.setHintTextColor(hintTextColor);
        setInputType();
        LinearLayout.LayoutParams lp = (LayoutParams) edt.getLayoutParams();
        lp.leftMargin = (int) text_margin_left;
        lp.topMargin = (int) text_margin_top;
        lp.bottomMargin = (int) text_margin_bottom;
        edt.setTextSize(TypedValue.COMPLEX_UNIT_PX, text_size);
    }

    private void setInputType() {
        if (inputType == 0) {
            edt.setInputType(InputType.TYPE_CLASS_NUMBER);
        } else if (inputType == 1) {
            edt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else if (inputType == 2) {
            edt.setInputType(InputType.TYPE_CLASS_TEXT);
        }
    }

    private void loadAttrs(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ClearEditText);
        hint = a.getString(R.styleable.ClearEditText_cet_hint);
        if (hint != null) {
            edt.setHint(hint);
        }
        int maxLenth = a.getInt(R.styleable.ClearEditText_cet_maxLenth, -1);
        if (maxLenth >= 0) {
            edt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLenth)});
        }
        textColor = a.getColor(R.styleable.ClearEditText_cet_textColor, textColor);
        hintTextColor = a.getColor(R.styleable.ClearEditText_cet_hintTextColor, hintTextColor);
        inputType = a.getInt(R.styleable.ClearEditText_cet_inputType, 0);
        text_margin_left = a.getDimensionPixelSize(R.styleable.ClearEditText_cet_text_margin_left, UIUtils.dip2Px(0));
        text_margin_top = a.getDimensionPixelSize(R.styleable.ClearEditText_cet_text_margin_top, UIUtils.dip2Px(11));
        text_margin_bottom = a.getDimensionPixelSize(R.styleable.ClearEditText_cet_text_margin_bottom, UIUtils.dip2Px(11));
        text_size = a.getDimensionPixelSize(R.styleable.ClearEditText_cet_text_size, UIUtils.dip2Px(14));
    }

    public void setHint(String hint) {
        if (hint == null) {
            return;
        }
        this.hint = hint;
        edt.setHint(hint);
    }

    public void setMaxLenth(int maxLenth) {
        if (maxLenth < 0) {
            return;
        }
        edt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLenth)});
    }

    public Editable getEditableText() {
        return edt.getEditableText();
    }

    public EditText getEdt() {
        return edt;
    }
}
